#include "engine.h"

int Engine::_size = 0;
int Engine::_maxsize = 0;
Engine **Engine::arr;

Engine::Engine(const std::string &model, const int &year) : _model(model), _year(year)
{
    // добавление в список
    Add();
}

Engine::~Engine()
{
    for (int i = 0; i < _size; ++i)
    {
        if (arr[i] == this)
        {
            for (int j = i; j < _size - 1; ++j)
            {
                arr[j] = arr[j + 1];
            }
            arr[_size - 1] = nullptr;
            --_size;
        }
    }
}

void Engine::print()
{
    for (int i = 0; i < _size; ++i)
    {
        arr[i]->Show();
        std::cout << std::endl;
    }
}

void Engine::Add()
{
    if (_maxsize == 0)
    {
        _maxsize = 5;
        arr = new Engine *[_maxsize];
        ++_size;
        arr[0] = this;
    }
    else if (_size == _maxsize)
    {
        _maxsize *= 2;
        Engine **array = new Engine *[_maxsize];
        for (int i = 0; i < _size; ++i)
        {
            array[i] = arr[i];
        }
        array[_size] = this;
        ++_size;
        delete[] arr;
        arr = array;
    }
    else
    {
        arr[_size] = this;
        ++_size;
    }
}
